define(['common/geometry/geometry2d'], function(geometry2d) {

module('geometry2d Vector');

test('add', function() {
  var vector1 = new geometry2d.Vector(1, 2);
  var vector2 = new geometry2d.Vector(3, 4);
  vector2.add(vector1);
  equal(4, vector2.x, 'wrong x value');
  equal(6, vector2.y, 'wrong y value');
});

test('normalize', function() {
  var vector = new geometry2d.Vector(5, 0);
  vector.normalize();
  equal(1, vector.x);
  equal(0, vector.y);
  equal(1, vector.length());
});

test('rotate 1/8', function() {
  var vector = new geometry2d.Vector(5, 0.23);
  var originalX = vector.x;
  var originalY = vector.y;
  var originalLength = vector.length();

  vector.rotate(1/8);
  ok(vector.x > 0);
  ok(vector.y < 0);
  almostEqual(originalLength, vector.length(), 0.001);

  vector.rotate(1/8);
  ok(vector.x > 0);
  ok(vector.y < 0);
  almostEqual(originalLength, vector.length(), 0.001);

  vector.rotate(1/8);
  ok(vector.x < 0);
  ok(vector.y < 0);
  almostEqual(originalLength, vector.length(), 0.001);

  vector.rotate(1/8);
  ok(vector.x < 0);
  ok(vector.y < 0);
  almostEqual(originalLength, vector.length(), 0.001);

  vector.rotate(1/8);
  ok(vector.x < 0);
  ok(vector.y > 0);
  almostEqual(originalLength, vector.length(), 0.001);

  vector.rotate(1/8);
  ok(vector.x < 0);
  ok(vector.y > 0);
  almostEqual(originalLength, vector.length(), 0.001);

  vector.rotate(1/8);
  ok(vector.x > 0);
  ok(vector.y > 0);
  almostEqual(originalLength, vector.length(), 0.001);

  vector.rotate(1/8);
  almostEqual(originalX, vector.x, 0.001);
  almostEqual(originalY, vector.y, 0.001);
});

test('rotate -1/8', function() {
  var vector = new geometry2d.Vector(5, 0.23);
  var originalX = vector.x;
  var originalY = vector.y;
  var originalLength = vector.length();

  vector.rotate(-1/8);
  ok(vector.x > 0);
  ok(vector.y > 0);
  almostEqual(originalLength, vector.length(), 0.001);

  vector.rotate(-1/8);
  ok(vector.x < 0);
  ok(vector.y > 0);
  almostEqual(originalLength, vector.length(), 0.001);

  vector.rotate(-1/8);
  ok(vector.x < 0);
  ok(vector.y > 0);
  almostEqual(originalLength, vector.length(), 0.001);

  vector.rotate(-1/8);
  ok(vector.x < 0);
  ok(vector.y < 0);
  almostEqual(originalLength, vector.length(), 0.001);

  vector.rotate(-1/8);
  ok(vector.x < 0);
  ok(vector.y < 0);
  almostEqual(originalLength, vector.length(), 0.001);

  vector.rotate(-1/8);
  ok(vector.x > 0);
  ok(vector.y < 0);
  almostEqual(originalLength, vector.length(), 0.001);

  vector.rotate(-1/8);
  ok(vector.x > 0);
  ok(vector.y < 0);
  almostEqual(originalLength, vector.length(), 0.001);

  vector.rotate(-1/8);
  almostEqual(originalX, vector.x, 0.001);
  almostEqual(originalY, vector.y, 0.001);
});

test('rotate 1/4', function() {
  var vector = new geometry2d.Vector(5, 0.23);

  vector.rotate(1/4);
  equal(0.23, vector.x);
  equal(-5, vector.y);

  vector.rotate(1/4);
  equal(-5, vector.x);
  equal(-0.23, vector.y);

  vector.rotate(1/4);
  equal(-0.23, vector.x);
  equal(5, vector.y);

  vector.rotate(1/4);
  equal(5, vector.x);
  equal(0.23, vector.y);
});

test('rotate -1/4', function() {
  var vector = new geometry2d.Vector(5, 0.23);

  vector.rotate(-1/4);
  equal(-0.23, vector.x);
  equal(5, vector.y);

  vector.rotate(-1/4);
  equal(-5, vector.x);
  equal(-0.23, vector.y);

  vector.rotate(-1/4);
  equal(0.23, vector.x);
  equal(-5, vector.y);

  vector.rotate(-1/4);
  equal(5, vector.x);
  equal(0.23, vector.y);
});

test('flip', function() {
  var vector = new geometry2d.Vector(2, 3);
  vector.flip();
  equal(-2, vector.x);
  equal(-3, vector.y);
});

test('length', function() {
  var vector = new geometry2d.Vector(4, 5);
  equal(Math.sqrt(16 + 25), vector.length());
});

test('distance', function() {
  var vector1 = new geometry2d.Vector(3, 4);
  var vector2 = new geometry2d.Vector(7, 9);
  equal(Math.sqrt(16 + 25), geometry2d.distance(vector1, vector2));
  equal(Math.sqrt(16 + 25), geometry2d.distance(vector2, vector1));
});

test('dot_perpendicular', function() {
  var vector1 = new geometry2d.Vector(1, 1);
  var vector2 = new geometry2d.Vector(1, -1);
  equal(0, geometry2d.dot(vector1, vector2));
  equal(0, geometry2d.dot(vector2, vector1));
});

test('dot_parallel', function() {
  var vector1 = new geometry2d.Vector(1, 1);
  var vector2 = new geometry2d.Vector(2, 2);
  equal(4, geometry2d.dot(vector1, vector2));
  equal(4, geometry2d.dot(vector2, vector1));
});

module('geometry2d LineSegment');

test('direction', function() {
  var lineSegment = new geometry2d.LineSegment(
    new geometry2d.Vector(2, 1),
    new geometry2d.Vector(2, 4));
  var direction = lineSegment.direction();
  equal(0, direction.x);
  equal(1, direction.y);
});

test('normal', function() {
  var lineSegment = new geometry2d.LineSegment(
    new geometry2d.Vector(0, 0),
    new geometry2d.Vector(2, 0));
  var normal = lineSegment.normal();
  equal(0, normal.x);
  equal(1, normal.y);
});

test('length', function() {
  var lineSegment = new geometry2d.LineSegment(
    new geometry2d.Vector(0, 1),
    new geometry2d.Vector(2, 1));
  equal(2, lineSegment.length());
});

test('lerp', function() {
  var lineSegment = new geometry2d.LineSegment(
    new geometry2d.Vector(0, 1),
    new geometry2d.Vector(2, 3));
  var middle = lineSegment.lerp(0.5);
  equal(1, middle.x);
  equal(2, middle.y);
});

});
